{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from datetime import datetime\n",
    "import backtrader as bt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data0 = bt.feeds.YahooFinanceData(dataname='YHOO',\n",
    "                                  fromdate=datetime(2011, 1, 1),\n",
    "                                  todate=datetime(2012, 12, 31))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "backtrader.feeds.yahoo.YahooFinanceData"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mplfinance as mpf\n",
    "import matplotlib.pyplot as plt\n",
    "import yfinance as yf\n",
    "import pandas as pd\n",
    "\n",
    "df = yf.download(\"AAPL\", start=\"2022-1-22\", end=\"2022-2-29\", interval='1m', progress=False)\n",
    "df.index = pd.to_datetime(df.index)\n",
    "df.index = df.index.tz_localize(None)\n",
    "\n",
    "df = df[:200] # Limit data to emphasize the lines in the graph.\n",
    "\n",
    "o_lines = []\n",
    "m_lines = []\n",
    "for c in df.columns[:4]:\n",
    "    if c == 'Open':\n",
    "        idx, price1, end, price2 = df[c].head(1).index[0], df[c].head(1)[0], df[c].tail(1).index[0], df[c].head(1)[0] \n",
    "        o_lines.append((idx, price1))\n",
    "        o_lines.append((end, price2))\n",
    "    if c == 'High':\n",
    "        idx, price1, end, price2 = df.loc[df[c].idxmax()].name, df.loc[df[c].idxmax()][c], df[c].tail(1).index[0], df.loc[df[c].idxmax()][c] \n",
    "        m_lines.append((idx, price1))\n",
    "        m_lines.append((end, price2))\n",
    "        # print(m_lines)\n",
    "\n",
    "mpf.plot(df, type='candle', alines=dict(alines=[o_lines, m_lines], colors=['b','r'], linewidths=2, alpha=0.4), style='yahoo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-5:\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\vnstudio\\lib\\threading.py\", line 917, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"C:\\vnstudio\\lib\\threading.py\", line 865, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"C:\\vnstudio\\lib\\site-packages\\multitasking\\__init__.py\", line 104, in _run_via_pool\n",
      "    return callee(*args, **kwargs)\n",
      "  File \"C:\\vnstudio\\lib\\site-packages\\yfinance\\multi.py\", line 190, in _download_one_threaded\n",
      "    timeout)\n",
      "  File \"C:\\vnstudio\\lib\\site-packages\\yfinance\\multi.py\", line 207, in _download_one\n",
      "    timeout=timeout)\n",
      "  File \"C:\\vnstudio\\lib\\site-packages\\yfinance\\base.py\", line 158, in history\n",
      "    end = int(_time.mktime(_time.strptime(str(end), '%Y-%m-%d')))\n",
      "  File \"C:\\vnstudio\\lib\\_strptime.py\", line 571, in _strptime_time\n",
      "    tt = _strptime(data_string, format)[0]\n",
      "  File \"C:\\vnstudio\\lib\\_strptime.py\", line 544, in _strptime\n",
      "    datetime_date(year, 1, 1).toordinal() + 1\n",
      "ValueError: day is out of range for month\n",
      "\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_7676/2431163697.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0myf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdownload\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"AAPL\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"2021-1-22\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"2022-2-29\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'1d'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprogress\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mto_datetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtz_localize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\vnstudio\\lib\\site-packages\\yfinance\\multi.py\u001b[0m in \u001b[0;36mdownload\u001b[1;34m(tickers, start, end, actions, threads, group_by, auto_adjust, back_adjust, progress, period, show_errors, interval, prepost, proxy, rounding, timeout, **kwargs)\u001b[0m\n\u001b[0;32m    110\u001b[0m                                    rounding=rounding, timeout=timeout)\n\u001b[0;32m    111\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mshared\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_DFS\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtickers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 112\u001b[1;33m             \u001b[0m_time\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.01\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    113\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    114\u001b[0m     \u001b[1;31m# download synchronously\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import mplfinance as mpf\n",
    "import matplotlib.pyplot as plt\n",
    "import yfinance as yf\n",
    "import pandas as pd\n",
    "\n",
    "df = yf.download(\"AAPL\", start=\"2021-1-22\", end=\"2022-2-29\", interval='1d', progress=False)\n",
    "df.index = pd.to_datetime(df.index)\n",
    "df.index = df.index.tz_localize(None)\n",
    "\n",
    "df = df[:200] # Limit data to emphasize the lines in the graph.\n",
    "\n",
    "o_lines = []\n",
    "m_lines = []\n",
    "for c in df.columns[:4]:\n",
    "    if c == 'Open':\n",
    "        idx, price1, end, price2 = df[c].head(1).index[0], df[c].head(1)[0], df[c].tail(1).index[0], df[c].head(1)[0] \n",
    "        o_lines.append((idx, price1))\n",
    "        o_lines.append((end, price2))\n",
    "    if c == 'High':\n",
    "        idx, price1, end, price2 = df.loc[df[c].idxmax()].name, df.loc[df[c].idxmax()][c], df[c].tail(1).index[0], df.loc[df[c].idxmax()][c] \n",
    "        m_lines.append((idx, price1))\n",
    "        m_lines.append((end, price2))\n",
    "        # print(m_lines)\n",
    "\n",
    "mpf.plot(df, type='candle', alines=dict(alines=[o_lines, m_lines], colors=['b','r'], linewidths=2, alpha=0.4), style='yahoo')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "0d2af4708e901420ca316528daed9d207adf237f59982b81f6b4928a0eab77f9"
  },
  "kernelspec": {
   "display_name": "Python 3.7.1 64-bit (system)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
